package com.foodplus.www.view.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.foodplus.www.api.ExceptionHandle;
import com.foodplus.www.api.ParamManager;
import com.foodplus.www.bean.login.MemberDataBean;
import com.foodplus.www.bean.login.MyAssetBean;
import com.foodplus.www.foodplus.MyApp;
import com.foodplus.www.foodplus.R;
import com.foodplus.www.presenter.UserDataPresenter;
import com.foodplus.www.view.activity.LoginActivity;
import com.foodplus.www.view.activity.SettingActivity;
import com.foodplus.www.view.impl.MyView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyFragment extends Fragment implements MyView {

    @BindView(R.id.iv_setting)
    ImageView ivSetting;
    @BindView(R.id.tv_login_now)
    TextView tvLoginNow;
    @BindView(R.id.tv_check_all)
    TextView tvCheckAll;
    @BindView(R.id.tv_pending_payment)
    TextView tvPendingPayment;
    @BindView(R.id.tv_pending_delivery)
    TextView tvPendingDelivery;
    @BindView(R.id.tv_pending_receiving)
    TextView tvPendingReceiving;
    @BindView(R.id.tv_pending_evaluate)
    TextView tvPendingEvaluate;
    @BindView(R.id.tv_refund)
    TextView tvRefund;
    @BindView(R.id.my_remainder)
    LinearLayout myRemainder;
    @BindView(R.id.my_voucher)
    LinearLayout myVoucher;
    @BindView(R.id.my_integral)
    LinearLayout myIntegral;
    @BindView(R.id.tv_my_collection)
    TextView tvMyCollection;
    @BindView(R.id.tv_my_activity)
    TextView tvMyActivity;
    @BindView(R.id.tv_invite_friends)
    TextView tvInviteFriends;
    @BindView(R.id.tv_available_balance)
    TextView tvAvailableBalance;
    @BindView(R.id.tv_voucher)
    TextView tvVoucher;
    @BindView(R.id.tv_point)
    TextView tvPoint;
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;

    private UserDataPresenter presenter;

    private boolean LoggedIn = false;


    public MyFragment() {
    }

    public static MyFragment newInstance() {

        Bundle args = new Bundle();

        MyFragment fragment = new MyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        //如果登录状态发生改变
        if (LoggedIn != MyApp.getInstance().isLoggedIn()) {
            LoggedIn = MyApp.getInstance().isLoggedIn();

            //刷新当前页面的各个数据
            refreshView(LoggedIn);
        }

    }

    private void refreshView(boolean loggedIn) {
        //loggedIn 为true 已登录,读取各项数据并显示
        //loggedIn 为false 退出登录,把各项数据恢复到初始值
        //这两种状态都需要处理
        if (loggedIn) {
            presenter = new UserDataPresenter(this);
            presenter.getMemberData(ParamManager.getInstance().getMemberParams());
            presenter.getMyAsset(ParamManager.getInstance().getMyAssetParams());
        } else {
            tvAvailableBalance.setText("-");
            tvVoucher.setText("-");
            tvPoint.setText("-");
            Glide.with(getContext()).load(R.mipmap.ic_my_not_logged).into(ivAvatar);
        }


    }

    @OnClick({R.id.iv_setting, R.id.tv_login_now, R.id.tv_check_all, R.id.tv_pending_payment, R.id.tv_pending_delivery, R.id.tv_pending_receiving, R.id.tv_pending_evaluate, R.id.tv_refund, R.id.my_remainder, R.id.my_voucher, R.id.my_integral, R.id.tv_my_collection, R.id.tv_my_activity, R.id.tv_invite_friends})
    public void onClick(View view) {

        if (view.getId() == R.id.iv_setting) {
            //设置
            Intent settingIntent = new Intent(getActivity(), SettingActivity.class);
            startActivity(settingIntent);
        }

        if (LoggedIn) {
            switch (view.getId()) {
                //立即登录
                case R.id.tv_login_now:
                    break;
                //查看全部
                case R.id.tv_check_all:
                    break;
                //待付款
                case R.id.tv_pending_payment:
                    break;
                //代发货
                case R.id.tv_pending_delivery:
                    break;
                //待收货
                case R.id.tv_pending_receiving:
                    break;
                //待评价
                case R.id.tv_pending_evaluate:
                    break;
                //退货
                case R.id.tv_refund:
                    break;
                //余额
                case R.id.my_remainder:
                    break;
                //代金券
                case R.id.my_voucher:
                    break;
                //积分
                case R.id.my_integral:
                    break;
                //我的收藏
                case R.id.tv_my_collection:
                    break;
                //我的活动
                case R.id.tv_my_activity:
                    break;
                //邀请朋友
                case R.id.tv_invite_friends:
                    break;
            }
        } else {
            switch (view.getId()) {

                //查看全部
                case R.id.tv_check_all:
                    //待付款
                case R.id.tv_pending_payment:
                    //代发货
                case R.id.tv_pending_delivery:
                    //待收货
                case R.id.tv_pending_receiving:
                    //待评价
                case R.id.tv_pending_evaluate:
                    //退货
                case R.id.tv_refund:
                    //余额
                case R.id.my_remainder:
                    //代金券
                case R.id.my_voucher:
                    //积分
                case R.id.my_integral:
                    //我的收藏
                case R.id.tv_my_collection:
                    //我的活动
                case R.id.tv_my_activity:
                    //邀请朋友
                case R.id.tv_invite_friends:
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                    //立即登录
                case R.id.tv_login_now:
                    Intent loginIntent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(loginIntent);
                    break;
            }
        }
    }

    @Override
    public void onMyAssetResult(MyAssetBean myAssetBean) {
        if (myAssetBean != null && myAssetBean.getCode() == 200) {
            MyAssetBean.DatasBean data = myAssetBean.getDatas();

            tvAvailableBalance.setText(data.getAvailable_rc_balance());
            tvVoucher.setText(data.getVoucher() + "");
            tvPoint.setText(data.getPoint() + "");
        }
    }

    @Override
    public void onMemberDataResult(MemberDataBean myMemberData) {
        if (myMemberData != null && myMemberData.getCode() == 200) {
            MemberDataBean.DatasBean data = myMemberData.getDatas();
            tvLoginNow.setText(data.getMember_nick());
            if (!"http://interface.yueshichina.com/data/upload/shop/avatar/".equals(data.getMember_avatar())) {
                Glide.with(getContext()).load(data.getMember_avatar()).into(ivAvatar);
            }
        }
    }

    @Override
    public void onFail(ExceptionHandle.ResponeThrowable e) {

    }

    @Override
    public void onFinish() {

    }
}
